package com.android.nfctouch.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.dialog.Comm_DialogFragment;
import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommLoading;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.ui.BaseFragment;
import com.android.baselibrary.util.Util;
import com.android.baselibrary.view.GridSpacingItemDecoration;
import com.android.baselibrary.view.MultiStateView;
import com.android.nfctouch.R;
import com.android.nfctouch.adapter.MyContactListAdapter;
import com.android.nfctouch.adapter.MyBannerImageAdapter;
import com.android.nfctouch.bean.BannerInfo;
import com.android.nfctouch.bean.ContactInfo;
import com.android.nfctouch.bean.UserInfo;
import com.android.nfctouch.constants.Constants;
import com.android.nfctouch.manager.API_HomeManager;
import com.android.nfctouch.manager.API_Manager;
import com.android.nfctouch.manager.SysConfigHelper;
import com.android.nfctouch.manager.UserManager;
import com.android.nfctouch.responce.BaseListData;
import com.android.nfctouch.ui.activity.AddContactActivity;
import com.android.nfctouch.ui.activity.BindActivationCodeActivity;
import com.android.nfctouch.ui.activity.ContactType_ListActivity;
import com.android.nfctouch.ui.activity.MyContactManageActivity;
import com.android.nfctouch.ui.activity.NFCActivity;
import com.gyf.immersionbar.ImmersionBar;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeMainFragment extends BaseFragment {

    @BindView(R.id.multiplestatusView)
    MultiStateView multiplestatusView;
    @BindView(R.id.ll_title_bar)
    LinearLayout ll_title_bar;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    int page = 1;
    int size = 20;

    MyContactListAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    protected void initView() {
        multiplestatusView.setText(R.id.tv_empty_text, "暂未添加联系方式");

        ImmersionBar.with(this).titleBar(ll_title_bar).statusBarDarkFont(true).navigationBarDarkIcon(true).init();
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerview.addItemDecoration(new GridSpacingItemDecoration(1, Util.dip2px(mContext, 10f), false));
        adapter = new MyContactListAdapter(mContext, callBack);
        recyclerview.setAdapter(adapter);

    }

    CommCallBack callBack = new CommCallBack() {
        @Override
        public void onResult(Object obj) {
            ContactInfo contactInfo = (ContactInfo) obj;
            Intent intent = new Intent(mContext, AddContactActivity.class);
            intent.putExtra("contactInfo", contactInfo);
            startActivity(intent);
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        if (mContext != null) {
            banner_list();
            home_contact_list();
            getUserInfo();
        }
    }

    //获取用户信息
    private void getUserInfo() {
        API_Manager.userInfo(mContext, new OkHttpCallBack<BaseResponce<UserInfo>>() {
            @Override
            public void onSuccess(BaseResponce<UserInfo> baseResponce) {
                UserManager.saveUserInfo(mContext, baseResponce.getData());
            }

            @Override
            public void onFailure(BaseResponce<UserInfo> baseResponce) {
                CommToast.showToast(mContext, baseResponce.msg);
            }
        });
    }

    @Override
    protected void initData() {

    }

    //获取banner
    private void banner_list() {
        API_HomeManager.banner_list(mContext, new OkHttpCallBack<BaseResponce<List<BannerInfo>>>() {
            @Override
            public void onSuccess(BaseResponce<List<BannerInfo>> baseResponce) {
                banner.setAdapter(new MyBannerImageAdapter(mContext, baseResponce.getData(), null))
                        .addBannerLifecycleObserver((LifecycleOwner) mContext)//添加生命周期观察者
                        .setIndicator(new CircleIndicator(mContext));
                if (banner.getAdapter().getItemCount() == 0) {
                    banner.setVisibility(View.GONE);
                } else {
                    banner.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(BaseResponce<List<BannerInfo>> baseResponce) {
                CommLoading.dismissLoading();
                CommToast.showToast(mContext, baseResponce.msg);
            }
        });
    }

    //获取联系方式列表
    private void home_contact_list() {
        API_HomeManager.home_contact_list(mContext, size, page, new OkHttpCallBack<BaseResponce<BaseListData<ContactInfo>>>() {
            @Override
            public void onSuccess(BaseResponce<BaseListData<ContactInfo>> baseResponce) {
                adapter.setData(baseResponce.getData().list);
                if (adapter.getItemCount() == 0) {
                    multiplestatusView.setViewState(MultiStateView.ViewState.EMPTY);
                } else {
                    multiplestatusView.setViewState(MultiStateView.ViewState.CONTENT);
                }
            }

            @Override
            public void onFailure(BaseResponce<BaseListData<ContactInfo>> baseResponce) {
                multiplestatusView.setViewState(MultiStateView.ViewState.ERROR);
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }

    @OnClick({R.id.ll_write, R.id.iv_add_contact, R.id.ll_buy, R.id.iv_manage_contact, R.id.ll_yulan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_write:    //写入
                if (TextUtils.isEmpty(UserManager.getUserInfo(mContext).activationCode)) {
                    //未激活
                    Comm_DialogFragment.showCommDialog(getActivity(), "", "账号未激活，请先激活", "去激活", "取消", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            StartActivity(BindActivationCodeActivity.class);
                        }
                    }, null);
                } else {
                    StartActivity(NFCActivity.class);
                }
                break;
            case R.id.iv_add_contact:   //添加联系方式
                StartActivity(ContactType_ListActivity.class);
                break;
            case R.id.ll_buy:   //购买
                String url = SysConfigHelper.getConfig(SysConfigHelper.Config_Purchase_link);
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                break;
            case R.id.iv_manage_contact:   //管理联系方式
                StartActivity(MyContactManageActivity.class);
                break;
            case R.id.ll_yulan:   //预览
                String userUrl = SysConfigHelper.getConfig(SysConfigHelper.Config_H5) + UserManager.getUserInfo(mContext).userId;
                Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(userUrl));
                startActivity(intent1);
                break;
        }
    }


}
